package com.ruoyi.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.pojo.PostbarMessage;
import com.ruoyi.service.PostbarMessageService;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/postbarmessage")
@CrossOrigin
public class PostBarMessageController {

    @Autowired
    private PostbarMessageService postbarMessageService;
    @Autowired
    private ISysUserService iSysUserService;

    @GetMapping("/list")
    public R<List<PostbarMessage>> list(PostbarMessage postbarMessage){
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("postbarid",postbarMessage.getPostbarid());
        List<PostbarMessage> list = postbarMessageService.list(wrapper);
        for (PostbarMessage i:list) {
            Integer userid = i.getUserid();
            SysUser sysUser = iSysUserService.selectUserById(userid.longValue());
            i.setUser(sysUser);
        }
        return R.success(list);
    }

    @PostMapping("/add")
    public R<String> add(@RequestBody PostbarMessage postbarMessage){
        postbarMessage.setCreatetime(new Date());
        int i = postbarMessageService.add(postbarMessage);
        if (i > 0) {
            return R.success("添加成功");
        }else{
            return R.error("添加失败");
        }
    }

}
